package com.qingfeng.core.dao;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qingfeng.core.config.database.BaseMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: 基类Dao
 * @author:LWY
 * @date: 2017/10/13 15:31
 * @modified:
 */
public abstract class BaseDao<T> {
    protected abstract BaseMapper<T> getBaseMapper();

    /**
     * 分页查询所有
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<T> get(Integer pageNum, Integer pageSize, Example example) {
        if (pageNum == null || pageSize == null) {
            pageNum = 0;
            pageSize = 0;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = getBaseMapper().selectByExample(example);
        PageInfo<T> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据对象条件查询
     *
     * @return
     */
    public List<T> select(T t) {
        List<T> list = getBaseMapper().select(t);
        return list;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public T byId(String id) {
        return getBaseMapper().selectByPrimaryKey(id);
    }


    /**
     * 删除
     *
     * @param ids id集合
     */
    public int del(String[] ids) {
        int i = 0;
        for (String id : ids) {
            i += getBaseMapper().deleteByPrimaryKey(id);
        }
        return i;
    }

    public int insert(T t) {
        return getBaseMapper().insertSelective(t);
    }

    public int insertList(List<T> ts) {
        return getBaseMapper().insertList(ts);
    }

    public int update(T t) {
        return getBaseMapper().updateByPrimaryKeySelective(t);
    }

    public int del(T t) {
        return getBaseMapper().delete(t);
    }
}
